<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表排序</title>
    <!-- 引入生产版本的vue.js -->
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="app">
        <h1>人员列表</h1>
        <!-- 模糊查询 -->
        <input type="text" placeholder="请输入内容" v-model="filterKey">
        <button @click="sortType=1">升序排列</button>
        <button @click="sortType=2">降序排列</button>
        <button @click="sortType=0">原顺序排列</button>

        <ul>
            <li v-for="item in filterArr_computed">
                {{item.name}}-{{item.age}}
            </li>
        </ul>
        
    </div>

    <script>
        const vm = new Vue({
            el:"#app",
            data() {
                return {
                    arr: [
                        {name:'张三',age:17},
                        {name:'李四',age:20},
                        {name:'老李头',age:16},
                        {name:'老三',age:18},
                    ],
                    filterKey:'',//模糊查询关键字
                    sortType:0,//排序类型
                };
            },

            computed:{
                filterArr_computed(){
                    const tempArr =  this.arr.filter(item => {
                        return item.name.indexOf(this.filterKey) != -1;
                    });
                    if(!this.sortType){
                       return tempArr; 
                    };
                    return tempArr.sort((p1,p2) => {
                        return this.sortType == 1 ? p1.age-p2.age : p2.age-p1.age; 
                    });
                }
            }
        })
    </script>
</body>
</html>